<!-- 支付 -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="支付单号" prop="documentNumber">
        <el-input
          v-model="queryParams.documentNumber"
          placeholder="请输入支付单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物流公司" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入物流公司"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="司机姓名" prop="driverName">
        <el-input
          v-model="queryParams.driverName"
          placeholder="请输入司机姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-thumb"
          size="mini"
          :disabled="single"
          @click="openPayType = true"
          v-hasPermi="['tws:payment:edit']"
        >支付</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['tws:payment:export']"
        >导出</el-button>
      </el-col> -->
    </el-row>

    <el-table v-loading="loading" :data="paymentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <el-table-column label="支付单号" align="center" prop="documentNumber" />
      <el-table-column label="物流公司" align="center" prop="companyName" show-overflow-tooltip />
      <el-table-column label="申请人" align="center" prop="applyUserName" show-overflow-tooltip />
      <el-table-column label="订单数" align="center" prop="orderTotal" />
      <el-table-column label="支付金额(元)" align="center" prop="monetaryAmount" />
      <el-table-column label="司机姓名" align="center" prop="driverName" />
      <el-table-column label="车牌号" align="center" prop="vehicleNumber" />
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="getOrderButton(scope.row)"
          >查看订单</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 选择支付方式对话框 -->
    <el-dialog title="选择支付方式" :visible.sync="openPayType" width="30%" center>
      <div>
        <el-radio-group v-model="payType" size="small">
          <el-radio label="1" border>线下支付</el-radio>
          <el-radio label="2" border disabled>在线支付</el-radio>
        </el-radio-group>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="choose">确 定</el-button>
        <el-button @click="openPayType = false">取 消</el-button>
      </span>
    </el-dialog>

    <!-- 添加或修改支付对话框 -->
    <el-dialog title="支付" :visible.sync="open" width="1000px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-descriptions title="" :column="3" border>
          <el-descriptions-item label="支付单号">{{ form.documentNumber }}</el-descriptions-item>
          <el-descriptions-item label="物流公司">{{ form.companyName }}</el-descriptions-item>
          <el-descriptions-item label="申请人">{{ form.applyUserName }}</el-descriptions-item>
          <el-descriptions-item label="订单数">{{ form.orderTotal }}</el-descriptions-item>
          <el-descriptions-item label="支付金额(元)">¥{{ form.monetaryAmount }}</el-descriptions-item>
          <el-descriptions-item label="司机姓名">{{ form.driverName }}</el-descriptions-item>
          <el-descriptions-item label="备注">{{ form.remark }}</el-descriptions-item>
        </el-descriptions>

        <el-row style="margin-top: 30px;">
          <el-col :span="12">
            <el-form-item label="收款方名称" prop="recipient">
              <el-input v-model="form.recipient" placeholder="请输入收款方名称" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="付款方式" prop="paymentMeansCode">
              <el-select v-model="form.paymentMeansCode" placeholder="请选择付款方式" clearable size="small">
                <el-option
                  v-for="dict in paymentMeanOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="" prop="receiptAccount">
              <span slot="label">
                收款帐户
                <el-tooltip effect="dark" placement="top" content="银行卡号或其他收款帐号">
                  <i class="el-icon-question"></i>
                </el-tooltip>
              </span>
              <el-input v-model="form.receiptAccount" placeholder="请输入收款帐户" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收款方银行代码" prop="bankCode">
              <el-select v-model="form.bankCode" placeholder="" filterable clearable size="small">
                <el-option
                  v-for="dict in bankOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="" prop="sequenceCode">
              <span slot="label">
                支付流水号
                <el-tooltip effect="dark" placement="top" content="银行或第三方支付平台的资金流水单号">
                  <i class="el-icon-question"></i>
                </el-tooltip>
              </span>
              <el-input v-model="form.sequenceCode" placeholder="请输入支付流水号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="支付时间" prop="payDate">
              <el-date-picker clearable size="small"
                v-model="form.payDate"
                type="datetime"
                placeholder="支付时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" style="text-align: center;">
        <el-button type="primary" :disabled="disabled" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog title="相关订单" :visible.sync="openOrder" width="1200px" append-to-body>
      <order-list ref="orderRef"></order-list>
    </el-dialog>
  </div>
</template>

<script>
import { payList, getPayment, payoff, delPayment } from "@/api/tws/payment";
import OrderList from '@/components/OrderList/index'

export default {
  name: "Payment",
  components: {
    OrderList
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 禁用弹窗新增或修改的确定按钮
      disabled: false,
      // 总条数
      total: 0,
      // 支付状态字典
      statusOptions: [],
      // 付款方式字典
      paymentMeanOptions: [],
      // 银行字典
      bankOptions: [],
      // 支付表格数据
      paymentList: [],
      // 是否显示弹出层
      openPayType: false,
      //相关订单弹出层
      openOrder:false,
      open: false,
      // 支付方式
      payType: '1',
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        documentNumber: null,
        carrier: null,
        actualCarrierId: null,
        vehicleNumber: null,
        vehiclePlateColorCode: null,
        driverId: null,
        driverName: null,
        orderTotal: null,
        monetaryAmount: null,
        applyUserName: null,
        payUserId: null,
        payUserName: null,
        payDate: null,
        payType: null,
        paymentMeansCode: null,
        recipient: null,
        receiptAccount: null,
        bankCode: null,
        sequenceCode: null,
        status: null,
        isSendToPro: null,
        sendToProDateTime: null,
        tenantId: null,
        companyName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        recipient: [
          { required: true, message: "收款方名称不能为空", trigger: "blur" }
        ],
        paymentMeansCode: [
          { required: true, message: "付款方式不能为空", trigger: ["blur", "change"] }
        ],
        receiptAccount: [
          { required: true, message: "收款账户不能为空", trigger: "blur" }
        ],
        sequenceCode: [
          { required: true, message: "支付流水号不能为空", trigger: "blur" }
        ],
        payDate: [
          { required: true, message: "支付时间不能为空", trigger: ["blur", "change"] }
        ]
      }
    };
  },
  created() {
    this.getDict();
    this.getList();
  },
  methods: {
    getDict() {
      // 支付状态字典
      this.getDicts("payment_status").then(response => {
        this.statusOptions = response.data;
      });
      // 付款方式字典
      this.getDicts("payment_mean").then(response => {
        this.paymentMeanOptions = response.data;
      });
      // 银行字典
      this.getDicts("bank_code").then(response => {
        this.bankOptions = response.data;
      });
    },
    /** 查询支付列表 */
    getList() {
      this.loading = true;
      payList(this.queryParams).then(response => {
        this.paymentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 支付状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        documentNumber: null,
        carrier: null,
        actualCarrierId: null,
        vehicleNumber: null,
        vehiclePlateColorCode: null,
        driverId: null,
        driverName: null,
        orderTotal: null,
        monetaryAmount: null,
        applyUserName: null,
        payUserId: null,
        payUserName: null,
        payDate: null,
        payType: null,
        paymentMeansCode: null,
        recipient: null,
        receiptAccount: null,
        bankCode: null,
        sequenceCode: null,
        status: null,
        isSendToPro: null,
        sendToProDateTime: null,
        remark: null,
        tenantId: null,
        companyName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 查看订单按钮操作 */
    getOrderButton(row){
      const id = row.id
      const type = 2; //1为开票 2为支付
      this.openOrder = true;
      this.$nextTick(() =>{
        this.$refs.orderRef.getList(id,type);
      })

    },
    // 选择支付方式
    choose() {
      this.openPayType = false;
      this.reset();
      const id = this.ids;
      let that = this;
      getPayment(id).then(response => {
        that.form = response.data;
        if(that.form.status !== that.$store.state.constants.payStatus_ysq) {
          that.$modal.msgError('该订单' + that.statusFormat(that.form.status))
          return
        } else {
          that.form.payType = that.payType;
          that.form.recipient = that.form.driverName;
          // 线下支付
          if(that.payType == '1') {
            that.open = true;
          } else if(that.payType == '2') {
            // 在线支付
          }
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.disabled = true;
      this.$refs["form"].validate(valid => {
        if (valid) {
          payoff(this.form).then(response => {
            this.$modal.msgSuccess("支付成功");
            this.disabled = false;
            this.open = false;
            this.getList();
          }).catch(err => {
            this.$modal.msgError(err.msg || '支付失败');
            this.disabled =false;
          })
        } else {
          this.disabled = false;
        }
      });
    }
  }
};
</script>
